﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace WebNext.UI.Utils.Validators
{
	public class EmailValidator : IValidator
	{
		private const string MatchEmailPattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

		private string email;

		public EmailValidator(string email)
		{
			this.email = email;
		}

		public virtual ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();
			if (string.IsNullOrWhiteSpace(this.email))
			{
				result.AddError(Properties.Resources.EmailCannotBeEmpty);
			}
			else if (this.email.Length > Constants.MaxEmailAddressLength)
			{
				result.AddError(Properties.Resources.EmailAddressTooLong, Constants.MaxEmailAddressLength);
			}
			else
			{
				if (!Regex.IsMatch(this.email, MatchEmailPattern))
				{
					result.AddError(Properties.Resources.EmailAddressHasIncorrectFormat);
				}
			}
			return result;
		}
	}
}